package com.easylinkin.sm.util;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import com.easylinkin.sm.entity.Language;

/**
 * 语言缓存类
 * <br>
 *
 * @author OuYangGuoDong
 * @version [版本号, 2015-10-29]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class LanguageFacade {

  private LanguageFacade() {

  }

  private static List<Language> allLanguages = null;

  public static List<Language> getAllLanguages() {
    return allLanguages;
  }

  public static void setAllLanguages(List<Language> allLanguages) {
    LanguageFacade.allLanguages = allLanguages;
  }

  /**
   * 获取语言ID
   *
   * @param code 语言标识
   * @return 语言ID
   * @author TianGanLin
   */
  public static Long getLanguageId(String code) {
    Language language = getLanguage(code);
    if (Objects.isNull(language)) {
      return null;
    }
    return language.getLanguageId();
  }

  /**
   * 获取语言
   *
   * @param code 语言标识
   * @return 语言实体类
   * @author TianGanLin
   */
  public static Language getLanguage(String code) {
    Optional<Language> first = allLanguages.stream()
        .filter(language -> Objects.equals(language.getCode(), code))
        .findFirst();
    return first.orElse(null);
  }
}
